<?php
class PdoMysql {
	
	private $_strHost = null;
	private $_intPort = null;
	private $_strUser = null;
	private $_strPassword = null;
	private $_strDbName = null;
	private $_objLinkId = null;
	
	public function connect($arrDsn) {
		$this->setDsn($arrDsn);
	}
	
	public function setDsn($arrDsn) {
		$this->_strHost = $arrDsn['HOST'];
		$this->_strPort = $arrDsn['PORT'];
		$this->_strDbName = $arrDsn['NAME'];
		$this->_strUser = $arrDsn['USER'];
		$this->_strPassword = $arrDsn['PASS'];
	}
	
	public function getLinkId() {
		try {
			$dbDsn = 'mysql:dbname=' . $this->_strDbName . ';host=' . $this->_strHost;
			$this->_objLinkId = new PDO($dbDsn, $this->_strUser, $this->_strPassword, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
			$this->_objLinkId->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			return $this->_objLinkId;
		} catch(PDOException $e) {
			Log::proxy(Log::_ERROR, $e->getMessage());
		}
	}
	
/**
	 * 添加数据
	 * @param string $strTable
	 * @param array $arrData
	 * @return int
	 */
	public function add($strTable, Array $arrData) {
		if (!empty($arrData)) {
			$arrFields = array();
			$arrValues = array();
			foreach ($arrData as $key => $value) {
				$arrFields[] = '`' . $key . '`';
				if (!get_magic_quotes_gpc()) {
					$arrValues[] = addslashes($value);
				}
			}
			$strFields = implode(',', $arrFields);
			$strValues = implode("','", $arrValues);
			$strSql = "INSERT INTO $strTable ($strFields) VALUES ('" . $strValues . "')";
			$boolRes = $this->query($strSql);
			if ($boolRes) {
				$insertId = $this->insertId();
			} else {
				$insertId = 0;
			}
		} else {
			$insertId = 0;
		}
		return $insertId;
	}
	
	
	/**
	 * 更新数据库操作
	 * @param unknown_type $strTables
	 * @param unknown_type $arrWhere
	 * @param unknown_type $arrData
	 * example :
	 * $arrWhere = array(
			 'name' => 'wufaliang',
			 'password' => 'meilishuo',
			 'hello' => 'world'
		)
		$arrOption = array(
			'order' => 'id desc, name desc'
			 'group' => 'name',
			 'limit' => '1,3',
			 'like' => '%wufaliang%'
		)
	 */
	protected function update($strTable, Array $arrData, Array $arrWhere, Array $arrOption = array()) {
		$strWhere = '1=1';
		$strData = '';
		if (!empty($arrWhere)) {
			if (!empty($arrWhere)) {
				foreach ($arrWhere as $key => $value) {
					$strWhere .= " AND `" . $key . "`='" . addslashes($value) . "' AND ";
				}
				$strWhere = substr($strWhere, 0, -5);
			}
			$strWhere = !empty($arrOption['like']) ? " AND like" . $arrOption['like'] : '';
			$strWhere = !empty($arrOption['group']) ? " AND GROUP BY " . $arrOption['group'] : '';
			$strWhere = !empty($arrOption['order']) ? " ORDER BY " . $arrOption['order'] : '';
			$strWhere = !empty($arrOption['limit']) ? " limit " . $arrOption['limit'] : '';
		}
		if (!empty($arrData)) {
			foreach ($arrData as $key => $value) {
				$strData .= "`" . $key . "`='" . $value . "',";
			}
			$strData = substr($strData, 0, -1);
		}
		$strSql = "UPDATE " . $strTable . " SET " . $strData . " WHERE " . $strWhere;
		$boolRes = $this->query($strSql);
		return $boolRes;
	}
	
	/**
	 * 删除数据
	 * @param string $strTable
	 * @param array $arrWhere
	 * @return boolean
	 */
	public function delete($strTable, Array $arrWhere) {
		if (!empty($arrWhere)) {
			$strWhere = '';
			foreach ($arrWhere as $key => $value) {
				$strWhere .= " AND `" . $key . "`='" . addslashes($value) . "' AND ";
			}
			$strWhere = substr($strWhere, 0, -5);
			$strSql = "DELETE FROM " . $strTable . " WHERE " . $strWhere;
			$boolRes = $this->query($strSql);
			return $boolRes;
		}
		return false;
	}
	
	/**
	 * 查询数据
	 * @param string $strTable
	 * @param array $arrWhere
	 * @param array $arrFileld
	 * @param array $arrOption
	 * @return array
	 */
	public function select($strTable, Array $arrWhere, Array $arrFileld = array(), Array $arrOption = array()) {
		$strWhere = '1=1';
		$strFileld = '';
		if (!empty($arrWhere)) {
			foreach ($arrWhere as $key => $value) {
				$strWhere .= " AND `" . $key . "`='" . addslashes($value) . "' AND ";
			}
			$strWhere = substr($strWhere, 0, -5);
			$strWhere = !empty($arrOption['like']) ? " AND like" . $arrOption['like'] : '';
			$strWhere = !empty($arrOption['group']) ? " AND GROUP BY " . $arrOption['group'] : '';
			$strWhere = !empty($arrOption['order']) ? " ORDER BY " . $arrOption['order'] : '';
			$strWhere = !empty($arrOption['limit']) ? " limit " . $arrOption['limit'] : '';
		}
		if (!empty($arrFileld)) {
			foreach ($arrFileld as $fileld) {
				$strFileld .= "`" . $fileld . "`,";	
			}
			$strFileld = substr($strFileld, 0, -1);
		} else {
			$strFileld = '*';
		}
		$strSql = "SELECT " . $strFileld . " FROM " . $strTable . " WHERE " . $strWhere;
		$objData = $this->query($strSql);
		while ($arrDataTemp = $this->fetchArray($objData)) {
			$arrData[] = $arrDataTemp;
		} 
		return $arrData;
	}
	
	/**
	 * 获取总数
	 * @param string $strTable
	 * @param array $arrWhere
	 * @param array $arrOption
	 * @return int
	 */
	public function total($strTable, Array $arrWhere, Array $arrOption = array()) {
		$strWhere = '1=1';
		$strFileld = '';
		if (!empty($arrWhere)) {
			foreach ($arrWhere as $key => $value) {
				$strWhere .= " AND `" . $key . "`='" . addslashes($value) . "' AND ";
			}
			$strWhere = substr($strWhere, 0, -5);
			$strWhere = !empty($arrOption['like']) ? " AND like" . $arrOption['like'] : '';
			$strWhere = !empty($arrOption['group']) ? " AND GROUP BY " . $arrOption['group'] : '';
			$strWhere = !empty($arrOption['order']) ? " ORDER BY " . $arrOption['order'] : '';
			$strWhere = !empty($arrOption['limit']) ? " limit " . $arrOption['limit'] : '';
		}
		$strSql = "SELECT count(*) AS total FROM " . $strTable . " WHERE " . $strWhere;
		$objData = $this->query($strSql);
		$arrdataTemp = $this->_objDb->fetchArray($objData);
	    return $arrdataTemp['total'];
	}
	
	public function beginTransaction() {
		$objLinkId = $this->getLinkId();
		if (!$objLinkId->inTransaction()) {
			$boolRes = $objLinkId->beginTransaction();
			if ($boolRes) {
				Log::fetal('事务开启失败');
				return false;
			} else {
				return true;
			}	
		}
	}
	
	public function commit() {
		$objLinkId = $this->getLinkId();
		$boolRes = $objLinkId->commit();
		if ($boolRes) {
			Log::fetal('事务提交失败');
			return false;
		} else {
			return true;
		}
	}
	
	public function rollBack() {
		$objLinkId = $this->getLinkId();
		$boolRes = $objLinkId->rollBack();
		if ($boolRes) {
			Log::fetal('事务回滚失败');
			return false;
		} else {
			return true;
		}
	}
}